package com.group4.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.group4.rbac.entity.Role;
import com.group4.rbac.mapper.RoleMapper;
import com.group4.rbac.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.util.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cloud
 * @since 2022-03-10
 */
@Slf4j
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    /**
     * 添加角色业务逻辑层
     * @param role
     * @return
     */
    @Override
    public ResponseResult add(Role role) {
        log.info("角色信息:{}",role);
        if(role == null || role.getRoleName() == null || role.getRoleName().trim().length()==0 || role.getRoleCode()==null || role.getRoleCode().trim().length()==0){
            return new ResponseResult(500,"添加信息不能为空");
        }
        role.setGmtCreate(new Date());
        role.setGmtModified(new Date());
        int insert = roleMapper.insert(role);

        if(insert == 0){
            return new ResponseResult(501,"添加信息失败");
        }
        return new ResponseResult(200,"添加角色成功");
    }

    /**
     * 修改角色信息
     * @param role
     * @return
     */
    @Override
    public ResponseResult update(Role role) {
        if(role==null|| role.getId() == null || role.getId().trim().length()==0){
            return new ResponseResult(500,"角色id为空");
        }
        int i = roleMapper.updateById(role);
        if(i == 0){
            return new ResponseResult(501,"修改失败");
        }
        QueryWrapper<Role> qw = new QueryWrapper<>();
        qw.eq("id",role.getId());
        Role role1 = roleMapper.selectOne(qw);
        return new ResponseResult(200,"修改成功",role1);
    }
}
